package com.njtoyo.taxi.admin.logic.allowance;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.njtoyo.taxi.admin.library.Const;
import com.njtoyo.taxi.admin.library.common.Enum;
import com.njtoyo.taxi.admin.library.common.util.SearchTimeType;
import com.njtoyo.taxi.admin.mapper.master.entity.RideOrder;
import com.njtoyo.taxi.admin.mapper.master.entity.ScanRideOrder;
import com.njtoyo.taxi.admin.mapper.master.enums.RideOrderState;
import com.njtoyo.taxi.admin.mapper.master.mapper.RideOrderMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.ScanRideOrderMapper;
import com.njtoyo.taxi.admin.rest.presenter.data.data_analysis.SearchTimeTypePresenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@DS(Const.PRIMARY_DS)
public class OrderLogic {

    @Autowired
    private RideOrderMapper rideOrderMapper;

    @Autowired
    private ScanRideOrderMapper scanRideOrderMapper;

    public Integer getCount(Enum.DateGroupType type) {
        SearchTimeTypePresenter timeRange = SearchTimeType.getStartAndEndByType(type);

        // 获取ride_order数量
        Integer rideCount = getRideOrderCount(timeRange.getStartTime(), timeRange.getEndTime());

        // 获取scan_ride_order数量
        Integer scanRideOrderCount = getScanRideOrderCount(timeRange.getStartTime(), timeRange.getEndTime());

        return rideCount + scanRideOrderCount;
    }

    public Integer getRideOrderCount(Date startTime, Date endTime) {
        String startTimeStr = DateUtil.formatDateTime(startTime);
        String endTimeStr = DateUtil.formatDateTime(endTime);

        // 获取ride_order数量
        Integer res = new LambdaQueryChainWrapper<>(rideOrderMapper)
                .eq(RideOrder::getState, RideOrderState.done)
                .eq(RideOrder::getRefunded, false)
                .between(RideOrder::getLastPayTime, startTimeStr, endTimeStr)
                .count();

        return res;
    }

    public Integer getRideOrderCount(String startTime, String endTime) {
        // 获取ride_order数量
        Integer res = new LambdaQueryChainWrapper<>(rideOrderMapper)
                .eq(RideOrder::getState, RideOrderState.done)
                .eq(RideOrder::getRefunded, false)
                .between(RideOrder::getLastPayTime, startTime, endTime)
                .count();

        return res;
    }

    public Integer getScanRideOrderCount(Date startTime, Date endTime) {
        Long startTimeStamp = startTime.getTime() / 1000;
        Long endTimeStamp = endTime.getTime() / 1000;

        // 获取scan_ride_order数量
        Integer res = new LambdaQueryChainWrapper<>(scanRideOrderMapper)
                .eq(ScanRideOrder::getIsPaid, true)
                .between(ScanRideOrder::getPaidTimestamp, startTimeStamp, endTimeStamp)
                .count();

        return res;
    }
}
